//
//  helpListPageVC.m
//  Cunli
//
//  Created by lilubin on 15/6/29.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "helpListPageVC.h"
#import "helpCellModel.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "HelpDetailVC.h"

NSString * const kHelplistCell = @"helplistCell";


@interface helpListPageVC ()
{
    NSMutableArray *_listDatas;
}

@end

@implementation helpListPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"使用帮助";
    [self setNavAttributes];
    [self registerTableViewCell];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    if (self = [super initWithStyle:UITableViewStylePlain]) {
        [self reloadListDatas];
    }
    return self;
}

- (void)registerTableViewCell
{
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHelplistCell];
}

- (void)initAllGlobalParams
{
    
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listDatas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHelplistCell forIndexPath:indexPath];
    helpCellModel *cellModel = _listDatas[indexPath.row];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = cellModel.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [self reloadDetailHelpInfo];
    HelpDetailVC *detailVC = [[HelpDetailVC alloc] init];
    helpCellModel *cellModel = _listDatas[indexPath.row];
    detailVC.itemID = cellModel.itemid;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)reloadListDatas
{
    _listDatas = [NSMutableArray arrayWithCapacity:1];
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:@"1" forKey:@"page"];
    [CheckParams setObject:@"10" forKey:@"pagesize"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"help.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        //        NSLog(@"Helplist: %@", result);
        NSString *response = result[@"response"];
        if ([response isEqualToString:@"help"]) {
            NSDictionary *helpListData = result[@"data"];
            NSArray *helpListArray = helpListData[@"helplist"];
            for(NSDictionary *listCellData in helpListArray)
            {
                helpCellModel *helpModel = [helpCellModel yy_modelWithJSON:listCellData];
                [_listDatas addObject:helpModel];
            }
            [self.tableView reloadData];
        }
        else if([response isEqualToString:@"error"]) {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *text = errorDic[@"text"];
            [Config showHUDWithText:text andImage:@"error"];
        }
        
    } failureAction:^{
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];
}

/*
 - (void)reloadDetailHelpInfo
 {
 NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:3];
 
 [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
 [CheckParams setObject:@"5" forKey:@"itemid"];
 NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
 [CheckParams setObject:resultSign forKey:@"sign"];
 
 [MyDataService requestURL:@"helpdetail.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
 NSLog(@"Detailhelp:%@", result);
 } failureAction:^{
 NSLog(@"Detailhelp error");
 }];
 
 }
 */

- (void)setNavAttributes
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
